package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Override
    @Transactional
    public void mySave(DishDto dishDto) {

        //不仅要往dish表中添加数据还要往dishFlavor表中添加数据
        dishMapper.insert(dishDto);

        //获取到菜品的id
        Long id = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());


        dishFlavorService.saveBatch(flavors);


    }


    /*
     * 回显数据
     * */
    @Override
    public DishDto mySetById(Long id) {

        //新建一个对象用于返回
        DishDto dishDto = new DishDto();
        Dish dish = dishMapper.selectById(id);

        //用对象拷贝将普通的属性都拷过来
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);

        return dishDto;
    }


    /*
     * 修改菜品
     * */
    @Override
    public void myUpdateById(DishDto dishDto) {

        //要修改两张表里的数据
        dishMapper.updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /*
     * 单个删除和批量删除
     * */
    @Override
    public void myDelete(List<Long> ids) {


        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(ids != null, Dish::getId, ids);
        List<Dish> list = this.list(queryWrapper1);

        for (Dish dish : list) {
            Integer status = dish.getStatus();

            if (status == 0) {

                LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(DishFlavor::getDishId, ids);

                dishMapper.deleteBatchIds(ids);
                dishFlavorService.remove(queryWrapper);

            }else {
                throw new CustomException("删除菜品中有正在售卖的菜品，无法删除");
            }

        }


    }


    /*
    * 停售起售
    * */
    @Override
    public void updateStatus(Integer status,List<Long> ids) {

        List<Dish> dishes = dishMapper.selectBatchIds(ids);

        for (Dish dish : dishes) {

            dish.setStatus(status);
            //操作好后再将数据更新回去
            this.updateById(dish);

        }

    }


}
